# Author: Andrea Junqueira
# Project: Janusz, Cunha, and Junqueira
# Goal: SI for Women Analysis
# First Version: Jan 23rd, 2024
# This Version: Jan 23rd, 2024

rm(list = ls())
options(scipen = 999)

library(here)
library(tidyverse)

load(here("Data","noncompetitive_candidates.RData"))

# Create plots

# All candidates

plot_all = noncompetitive_candidates %>%
  dplyr::filter(sample %in% c("All Candidates","Less than 10% of quota","Less than 1% of quota")) %>%
  group_by(ANO_ELEICAO) %>%
  mutate(perc = paste0(round(count/max(count)*100,1),"%") ) %>%
  ungroup() %>%
  mutate(sample = factor(sample, levels = c("All Candidates","Less than 10% of quota","Less than 1% of quota")))

ggplot(plot_all, aes(x = as.factor(ANO_ELEICAO), y = count, fill = sample, label = perc)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "",
       x = "",
       y = "Number of Candidates\n") +
  theme_bw() +
  scale_fill_grey(start = 0, end = .75) +
  geom_col(position = 'dodge') + 
  geom_text(position = position_dodge(width = .9),    
            vjust = -0.5,   
            size = 4.5) +
  theme(legend.position = "bottom") +
  theme(legend.title=element_blank(),
        text = element_text(size=20)) +
  scale_y_continuous(labels = scales::comma)

ggsave(here("plots_tables","noncompetitive_all.pdf"), width = 30, height = 20, units = "cm")



# Women candidates

plot_women = noncompetitive_candidates %>%
  dplyr::filter(sample %in% c("Women Candidates","Less than 10% of quota (Women)","Less than 1% of quota (Women)")) %>%
  group_by(ANO_ELEICAO) %>%
  mutate(perc = paste0(round(count/max(count)*100,1),"%") ) %>%
  ungroup()

plot_women$sample[plot_women$sample=="Women Candidates"] = "All Women Candidates"
plot_women$sample[plot_women$sample=="Less than 10% of quota (Women)"] = "Less than 10% of quota"
plot_women$sample[plot_women$sample=="Less than 1% of quota (Women)"] = "Less than 1% of quota"

plot_women = plot_women %>%
  mutate(sample = factor(sample, levels = c("All Women Candidates","Less than 10% of quota","Less than 1% of quota")))

ggplot(plot_women, aes(x = as.factor(ANO_ELEICAO), y = count, fill = sample, label = perc)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "",
       x = "",
       y = "Number of Women Candidates\n") +
  theme_bw() +
  scale_fill_grey(start = 0, end = .75) +
  geom_col(position = 'dodge') + 
  geom_text(position = position_dodge(width = .9),    
            vjust = -0.5,   
            size = 4.5) +
  theme(legend.position = "bottom") +
  theme(legend.title=element_blank(),
        text = element_text(size=20)) +
  scale_y_continuous(labels = scales::comma)

ggsave(here("plots_tables","noncompetitive_women.pdf"), width = 30, height = 20, units = "cm")


# Black candidates

plot_black = noncompetitive_candidates %>%
  dplyr::filter(sample %in% c("Black Candidates","Less than 10% of quota (Black)","Less than 1% of quota (Black)")) %>%
  group_by(ANO_ELEICAO) %>%
  mutate(perc = paste0(round(count/max(count)*100,1),"%") ) %>%
  ungroup()

plot_black$sample[plot_black$sample=="Black Candidates"] = "All Afro-Brazilian Candidates"
plot_black$sample[plot_black$sample=="Less than 10% of quota (Black)"] = "Less than 10% of quota"
plot_black$sample[plot_black$sample=="Less than 1% of quota (Black)"] = "Less than 1% of quota"

plot_black = plot_black %>%
  mutate(sample = factor(sample, levels = c("All Afro-Brazilian Candidates","Less than 10% of quota","Less than 1% of quota")))

ggplot(plot_black, aes(x = as.factor(ANO_ELEICAO), y = count, fill = sample, label = perc)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "",
       x = "",
       y = "Number of Afro-Brazilian Candidates\n") +
  theme_bw() +
  scale_fill_grey(start = 0, end = .75) +
  geom_col(position = 'dodge') + 
  geom_text(position = position_dodge(width = .9),    
            vjust = -0.5,   
            size = 4.5) +
  theme(legend.position = "bottom") +
  theme(legend.title=element_blank(),
        text = element_text(size=20)) +
  scale_y_continuous(labels = scales::comma)

ggsave(here("plots_tables","noncompetitive_black.pdf"), width = 30, height = 20, units = "cm")
